Get Incident Count By Team For Particular Period

I have a use case where for a particular time period, I need to get a count of incidents by team. Do not require any other details.

I currently use the following in Python.

from datetime import datetime, timedelta
import requests

BaseURL = “https://api.pagerduty.com/”
headers = {“Content-Type”: “application/json”,
“Accept”: “application/vnd.pagerduty+json;version=2”,
“Authorization”: “Token token=my token”}

DateNow = datetime.utcnow().date()

def GetIncidentCount(since, teamid):

querystring = {"since": since, "team_ids[]": teamid}
url = BaseURL + "incidents/count"
count = (requests.get(url, headers=headers, params=querystring)).json()['total']
return count

for n in range(30, 60):
DateSince = datetime.utcnow().date() - timedelta(n)
IncidentCount = GetIncidentCount(DateSince, “XXXXXX”)
ResultsRetStr = f’{n} days - XXXXXX {IncidentCount:>5}’
print(ResultsRetStr)

This seems to work fine up to the default 30 days. However as the number of days gets higher, the count fluctuates and eventually gets lower.

eg:

30 days - XXXXXX 588
31 days - XXXXXX 598
32 days - XXXXXX 602
33 days - XXXXXX 575
34 days - XXXXXX 576
35 days - XXXXXX 574
36 days - XXXXXX 570
37 days - XXXXXX 580
38 days - XXXXXX 609
39 days - XXXXXX 627
40 days - XXXXXX 638
41 days - XXXXXX 644
42 days - XXXXXX 624
43 days - XXXXXX 616
44 days - XXXXXX 618
45 days - XXXXXX 627
46 days - XXXXXX 628
47 days - XXXXXX 618
48 days - XXXXXX 613
49 days - XXXXXX 502
50 days - XXXXXX 318
51 days - XXXXXX 267
52 days - XXXXXX 202
53 days - XXXXXX 200
54 days - XXXXXX 206
55 days - XXXXXX 204
56 days - XXXXXX 205
57 days - XXXXXX 200
58 days - XXXXXX 201
59 days - XXXXXX 178

What am I doing wrong?

Hi Joe! In this part of the API, the results default to one-month-forward from your “since” date.

So your request is close; but you’re getting a sliding month-long window.

You’ll want to add an “until” parameter set to today’s date to your query to get all of the incidents starting at your since date and running through today. That should get it to work!

–mandi